% template and signal initializing
step = 0.01;
% template - canonacal gaussian
[xpsi psi] = template3(step);
% Correlation theshold level
CorrelationThresholdLevel = .3:.1:.7;
% maximum derivation in
trustedDerivation = 0.1;
% number of gaussians in experiments
gaussiansNumber = 10;
% S - number of statistic iterations (size of samlpe)
S = 100;

% ================= clean signal matching =================================

% effectivity results initialization

CorrelationFindedGaussiansNumber = zeros(1, length(CorrelationThresholdLevel));
CorrelationWrongGaussiansNumber = zeros(1, length(CorrelationThresholdLevel));

for i = 1: length(CorrelationThresholdLevel)
    for count = 1:S
        % statistic experiment
        % signal generating - sum of gaussians that are generated randomly
        [x sign means fmeans] = signal8(step, gaussiansNumber);
        % getting maximums of clean signal
        [xSignMax SignMax] = maxEdges(x, sign, .8 * min(fmeans));
        
        % ===== Signal cross-correlation ==================================
        r = correlation(sign, psi);
        scaledCorrelationEffectogram = correlationEffectogramScaling(x, r, xpsi);
        % getting maximums of thresholded effectogram
        [trustedMaximums ~] = maxEdges(x, scaledCorrelationEffectogram, CorrelationThresholdLevel(i));
        % getting number of finded gaussians
        numberFindedGaussians = searchEffectivity(xSignMax, trustedDerivation, trustedMaximums);
        CorrelationFindedGaussiansNumber(i) = CorrelationFindedGaussiansNumber(i) + numberFindedGaussians;
        % getting number of wrong-found gaussians
        CorrelationWrongGaussiansNumber(i) = CorrelationWrongGaussiansNumber(i)...
            + (length(trustedMaximums) - numberFindedGaussians);
    end;
end;

% getting average values
CorrelationFindedGaussiansNumber = CorrelationFindedGaussiansNumber / S;

CorrelationWrongGaussiansNumber = CorrelationWrongGaussiansNumber / S;

% plotting results
figure;
plot(CorrelationThresholdLevel, CorrelationFindedGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Signal cross-correlation');
xlabel('Trust level to Cross-correlation method');
ylabel('Number of found gaussians');

figure;
plot(CorrelationThresholdLevel, CorrelationWrongGaussiansNumber);
grid on;
title('Template Matching methods for clean signal');
legend('Signal cross-correlation');
xlabel('Trust level to Cross-correlation method');
ylabel('Number of wrong-found gaussians');